<?php
/**
 * User:姜亮
 * Date: 2018/8/31 ;Time: 10:41;
 * 用户管理
 */

namespace app\admin\controller;

use think\Controller;
use think\Config;
use app\admin\model\User as UserModel;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;

class User extends Controller
{
    //显示
    public function show()
    {
        $userInfoAll = UserModel::getUserInfoRole();//查询所有用户的信息
        $this->assign([
            'userInfos' => $userInfoAll,
            'count' => count($userInfoAll)
        ]);
        return $this->fetch('admin-list');
    }

    //添加
    public function admin_add()
    {
        $tag = input('user_add');
        if (empty($tag)) {
            //查询所有角色
            $authGroup = AuthGroup::all(function ($query) {
                $query->order('id', 'asc');
            });
            $this->assign('authGroup', $authGroup);
            return $this->fetch('admin-add');
        } else {
            $form=input('post.');
            $userModel = new UserModel();
            $userModel->username = $form['adminName'];
            $userModel->password = md5($form['password'] . Config::get('login_admin')['crypt']);
            $userModel->token = Config::get('login_admin')['crypt'];
            $userModel->alias_name = $form['alias_name'];
            $userModel->gender = $form['sex'];
            $userModel->phone_number = $form['phone'];
            $userModel->email = $form['email'];
            $userModel->create_time = date("Y-m-d H:i:s");
            $userModel->update_time = date("Y-m-d H:i:s");
            $res1 = $userModel->save();
            if ($res1 !== false) {
                $authGroupAccess=new AuthGroupAccess();
                $authGroupAccess->uid=$userModel->id;
                $authGroupAccess->group_id=$form['adminRole'];
                $res2 = $authGroupAccess->save();
                if ($res2 !== false) {
                    return xmall_msg(200, 'ok');
                }else{
                    return xmall_msg(201, 'fail');
                }
            } else {
                return xmall_msg(201, 'fail');
            }
        }
    }

    //修改
    public function admin_update()
    {
        $userInfo = input('userInfo');//userInfo
        if (!empty($userInfo)){
            //显示
            $id = input('id');
            $userModel = UserModel::getUserInfoRole($id);
            $authGroup = AuthGroup::where('id', '>=', 1)->select();
            $this->assign([
                'user' => $userModel[0],
                'authGroup' => $authGroup,
            ]);
            return $this->fetch('index/userInfo');
        }else{
            $tag = input('tag');//显示页面还是更新
            if (!empty($tag)) {//更新
                $form = input('post.');
                //更新
                $userModel = UserModel::get($form['id']);
                $userModel->username = $form['adminName'];
                $userModel->password = md5($form['password'] . $userModel->token);
                $userModel->gender = $form['sex'];
                $userModel->phone_number = $form['phone'];
                $userModel->email = $form['email'];
                $userModel->alias_name = $form['alias_name'];
                $userModel->update_time = date("Y-m-d H:i:s");
                $res1 = $userModel->save();
                $authGroupAccess = AuthGroupAccess::get($form['id']);
                $authGroupAccess->group_id = $form['adminRole'];
                $res2 = $authGroupAccess->save();
                if ($res1 !== false && $res2 !== false) {
                    return xmall_msg(200, 'ok');
                } else {
                    return xmall_msg(201, 'fail');
                }
            }
            else {
                //显示
                $id = input('id');
                $userModel = UserModel::getUserInfoRole($id);
                $authGroup = AuthGroup::where('id', '>=', 1)->select();
                $this->assign([
                    'user' => $userModel[0],
                    'authGroup' => $authGroup,
                ]);
                return $this->fetch('admin-update');
            }
        }
    }

    //删除
    public function admin_delete()
    {
        $ids=input('ids');
        if (empty($ids)){
            $id=input('id');
            $res1=UserModel::get($id)->delete();
            $res2=AuthGroupAccess::where('uid','=',$id)->delete();
            if ($res1!==false&&$res2 !== false) {
                return xmall_msg(200, 'ok');
            }else{
                return xmall_msg(201, 'fail');
            }
        }else{
            //删除多个
            $res1=UserModel::destroy($ids);
            $idsArray=explode(',',$ids);
            foreach ($idsArray as $item) {
                AuthGroupAccess::where('uid','=',$item)->delete();
            }
            if ($res1>0){
                return xmall_msg(200, 'ok');
            }else{
                return xmall_msg(201, 'fail');
            }
        }

    }

    //启用停用
    public function switchState()
    {
        $id = input('id');
        $switchState = input('switchState');
        $userModel = UserModel::get($id);
        $userModel->status = $switchState;
        $res = $userModel->save();
        if ($switchState == 0) {
            $msg = '停止';
        } else {
            $msg = '启用';
        }
        if ($res) {
            return xmall_msg(200, $msg . '成功!');
        } else {
            return xmall_msg(201, $msg . '失败!');
        }

    }

}